package handlers;

import java.io.IOException;

import server.Webserver;

public class Handlers {
    public static final String CRLF = "\r\n";

    public static String headerHandler(int return_code) {
        String s = "HTTP/1.1 ";
        switch (return_code) {
            case 200:
                s = s + "200 OK" + CRLF;
                break;
            case 500:
                s = s + "500 Internal Server Error" + CRLF;
                break;
            case 501:
                s = s + "501 Not Implemented" + CRLF;
                break;
        }
        s = s + "Connection: close" + CRLF;
        s = s + "Server: FancyChatV1.0" + CRLF;
        s = s + "Content-Type: text/html" + CRLF;
        s = s + CRLF;
        return s;
    }

    // This handler deals with finding names
    // /chat/findname?name=NIMI&myip=22.22.33.44:6666&ttl=TIMETOLIVE
    public static String findnameHandler(String request) {
        String subr = request.substring(request.indexOf('?') + 1);
        String[] params = subr.split("&");
        String nimi = params[0].split("=")[1];
        String address = params[1].split("=")[1];
        int ttl = Integer.parseInt(params[2].split("=")[1]);
        return null;
    }

    //This handler asks for the contact list of your friends
    //http://11.22.33.44:PORT/chat/asknames?ttl=TIMETOLIVE
    public static String asknamesHandler(String request) {
    	String body = "";
		try {
			body = Contacts.getContactFile();
		} catch (IOException e){}
        String subr = request.substring(request.indexOf('?') + 1);
        int ttl = Integer.parseInt(subr.split("=")[1]);
        return body;
    }

    public static String sendnameHandler(String request) {
        String subr = request.substring(request.indexOf('?') + 1);
        String[] params = subr.split("&");
        String nimi = params[0].split("=")[1];
        String address = params[1].split("=")[1];
        int ttl = Integer.parseInt(params[2].split("=")[1]);
        return null;
    }

    public static String sendmessageHandler(String request, Webserver ws) {
        String subr = request.substring(request.indexOf('?') + 1);
        String[] params = subr.split("&");
        String nimi = params[0].split("=")[1];
        String address = params[1].split("=")[1];
        int ttl = Integer.parseInt(params[2].split("=")[1]);
        return null;
    }
    
    public static String responseHandler(int return_code, int req_type, String request, Webserver ws){
    	String header = headerHandler(return_code);
    	String body = "";
    	switch (req_type) {
        case 1:
            body = findnameHandler(request);
            break;
        case 2:
            body = asknamesHandler(request);
            break;
        case 3:
            body = sendnameHandler(request);
            break;
        case 4:
        	body = sendmessageHandler(request, ws);
        	break;
        	
    }
    	return header+body;
    }
}
